/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_NETWORK_DOMAINSOCKET_H_
#define _HDFS_LIBHDFS3_NETWORK_DOMAINSOCKET_H_

#include <kmhdfs/network/tcp_socket.h>

namespace Hdfs {
namespace Internal {

/**
 * A Domain socket client
 */
class DomainSocketImpl : public TcpSocketImpl {
 public:
  /**
   * Construct a Socket object.
   * @throw nothrow
   */
  DomainSocketImpl();

  /**
   * Destroy a DomainSocketImpl instance.
   */
  ~DomainSocketImpl();

  /**
   * Connection to a domain socket server.
   * @param host The host of server.
   * @param port The port of server.
   * @param timeout The timeout interval of this read operation, negative
   * means infinite.
   * @throw HdfsNetworkException
   * @throw HdfsTimeout
   */
  void connect(const char *host, int port, int timeout);

  /**
   * Connection to a domain socket server.
   * @param host The host of server.
   * @param port The port of server.
   * @param timeout The timeout interval of this read operation, negative
   * means infinite.
   * @throw HdfsNetworkException
   * @throw HdfsTimeout
   */
  void connect(const char *host, const char *port, int timeout);

  /**
   * Connection to a domain socket server.
   * @param paddr The address of server.
   * @param host The host of server used in error message.
   * @param port The port of server used in error message.
   * @param timeout The timeout interval of this read operation, negative
   * means infinite.
   * @throw HdfsNetworkException
   * @throw HdfsTimeout
   */
  void connect(struct addrinfo *paddr, const char *host, const char *port,
               int timeout);

  /**
   * Read file descriptors from domain socket.
   *
   * @param fds buffer to hold the received file descriptors.
   * @param nfds number of file descriptors needs to receive.
   * @param buffer the buffer to receive data
   * @size  buffer size to receive data
   *
   * @return return the number of received bytes.
   */
  int32_t receiveFileDescriptors(int fds[], size_t nfds, char *buffer,
                                 int32_t size);
};
}
}

#endif /* _HDFS_LIBHDFS3_NETWORK_DOMAINSOCKET_H_ */
